package com.zpp.system.controller.sysuser;

import com.zpp.common.core.constants.HttpConstants;
import com.zpp.common.core.controller.BaseController;
import com.zpp.common.core.domain.Result;
import com.zpp.common.core.domain.vo.LoginUserVO;
import com.zpp.system.domain.sysuser.dto.LoginDTO;
import com.zpp.system.domain.sysuser.dto.SysUserSaveDTO;
import com.zpp.system.domain.sysuser.vo.SysUserVO;
import com.zpp.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysUser")
@Tag(name = "管理员接口")  //接口分组标签
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 管理员登录接口
     * 接口地址：system/sysUser/login
     * post请求方式会将账号密码放到body当中，不会显示到url直接看到不安全
     */
    @PostMapping("/login")
    //以下是swagger注解能够自动生成接口文档
    @Operation(summary = "管理员登录", description = "根据账号密码进行管理员登录")  //接口功能描述
    @ApiResponse(responseCode = "1000", description = "操作成功")              //响应结果描述
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102",description = "用户名不存在")
    @ApiResponse(responseCode = "3103",description = "用户名或密码错误")
    public Result<String> login(@RequestBody @Validated LoginDTO loginDTO) {
        return sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());
    }

    /**
     * 管理员退出登录接口
     * 接口地址：system/sysUser/logout
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toResult(sysUserService.logout(token));
    }



    //接口地址system/sysUser/info，获取到 当前登录用户的昵称
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return sysUserService.info(token);
    }


    @Operation(summary = "新增管理员", description = "根据提供的信息新增管理员⽤户")
    @PostMapping("/add")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤户已存在")
    public Result<Void> add(@RequestBody @Validated SysUserSaveDTO saveDTO) {
        return toResult(sysUserService.add(saveDTO));
    }


    @DeleteMapping("/{userId}")
    @Operation(summary = "删除⽤户", description = "通过⽤户id删除⽤户")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.PATH, description = "⽤户ID")
    })
    @ApiResponse(responseCode = "1000", description = "成功删除⽤户")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤户不存在")
    public Result<Void> delete(@PathVariable Long userId) {
        return null;
    }

    @Operation(summary = "⽤户详情", description = "根据查询条件查询⽤⼾详情")
    @GetMapping("/detail")
    @Parameters(value = {
             @Parameter(name = "userId", in = ParameterIn.QUERY, description = "⽤户ID"),
             @Parameter(name = "sex", in = ParameterIn.QUERY, description = "⽤户性别")
    })
    @ApiResponse(responseCode = "1000", description = "成功获取⽤户信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤户不存在")
     public Result<SysUserVO> detail(Long userId, @RequestParam(required = false) String sex) {
        return null;
     }
}
